package com.example.tvapp.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
 * 规范的存储和读取数据
 * */
public class FormatSaveUtil
{

    public static void main(String[] d)
    {
        String path="C:\\Users\\ASUS\\Desktop\\COLLECTS.txt";
        save(path,new String[]{"https://m.muyy.cc/vplay/159597-4-4.html","绷不住了","第四期"});
        save(path,new String[]{"https://m.muyy.cc/vplay/159597-4-4.html","绷不住了","第四期"});
        save(path,new String[]{"https://m.muyy.cc/vplay/159597-4-4.html","绷不住了","第四期"});
    }
    public static String separatorOfUnite="-----HUANGWEIMIN-----";
    public static String separatorOfElem="---HUWNGWEIMIN---";
    /**
     * 将字符数组中的数据作为一个单元以指定格式保存
     * @param savePath 保存路径
     *参考值：C:\Users\ASUS\Desktop\JxBrowser\TranslateDate.txt、.\Translate.txt
     * @param saveText 保存一个独立单位，这个独立单位中含有其他小单位
     * 参考值：String[] save={"giao","giao"};
     *每个单元保存格式：
     *      ----------
     *      giao
     *      -----
     *      giao
     *      -----
     *      ----------
     * */
    public static boolean save(String savePath, String[] saveText)
    {
        try
        {
            File file = new File(savePath);
            if (!file.exists())
            {
                file.createNewFile();
            }
            FileWriter fileWriter=new FileWriter(file,true);
            fileWriter.append(separatorOfUnite+"\n");
            for(int i=0;i<saveText.length;i++)
            {

                fileWriter.append(saveText[i]+"\n"+separatorOfElem+"\n");
            }
            fileWriter.append(separatorOfUnite+"\n");
            fileWriter.flush();
            fileWriter.close();
            return true;
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }
    /**
     * 读取以save()函数确定的规则保存的文件
     * 参考用法：
     *      Map<Integer, String[]> re=read("C:\\Users\\ASUS\\Desktop\\JxBrowser\\TranslateDate.txt");
     *         for(Integer key:re.keySet())
     *         {
     *             int k=re.get(key).length;
     *             System.err.println("第"+key+"条记录");
     *             for(int i=0;i<k;i++)
     *             {
     *                 System.err.println(re.get(key)[i]);
     *             }
     *         }
     * */
    public static Map<Integer, String[]> read(String path)
    {
        try
        {
            Map<Integer,String[]> result=new HashMap<>();
            File file=new File(path);
            BufferedReader bufferedReader=new BufferedReader(new FileReader(file));
            String s;
            String all="";
            while((s= bufferedReader.readLine())!=null)
            {
                all=all+s+"\n";
            }
            String[] r=all.split(separatorOfUnite);
            int index=0;
            for(int i=0;i<r.length;i++)
            {
                if(!Objects.equals(r[i], "\n")&&!r[i].equals(""))
                {
                    String[] imm = r[i].split(separatorOfElem);
                    result.put(index, imm);
                    index++;
                }
            }
            bufferedReader.close();
            return result;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }
}
